package com.prs.crm.action.online;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.online.ForumContent;
import com.prs.crm.domain.online.ForumContentReply;
import com.prs.crm.domain.online.ForumManager;
import com.prs.crm.service.online.ForumContentReplyService;
import com.prs.crm.service.online.ForumContentService;
import com.prs.crm.service.online.ForumManagerService;

public class ForumContentAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private ForumContent forumContent;
	private ForumContentService forumContentService;
	
	private ForumContentReply forumContentReply;
	private ForumContentReplyService forumContentReplyService;
	
	private List<ForumContentReply> reply;
	private File upload;
	private String uploadFileName;
	private String title;
	
	private PaginationSupport pagination;
	
	private String contentIds;
	
	private ForumManagerService forumManagerService;
	private ForumManager forumManager;
	private List<ForumManager> forum;
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public List<ForumManager> getForum() {
		return forum;
	}
	public void setForum(List<ForumManager> forum) {
		this.forum = forum;
	}
	public ForumManager getForumManager() {
		return forumManager;
	}
	public void setForumManager(ForumManager forumManager) {
		this.forumManager = forumManager;
	}
	public ForumManagerService getForumManagerService() {
		return forumManagerService;
	}
	public void setForumManagerService(ForumManagerService forumManagerService) {
		this.forumManagerService = forumManagerService;
	}
	public String getContentIds() {
		return contentIds;
	}
	public void setContentIds(String contentIds) {
		this.contentIds = contentIds;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public ForumContent getForumContent() {
		return forumContent;
	}
	public void setForumContent(ForumContent forumContent) {
		this.forumContent = forumContent;
	}
	public ForumContentService getForumContentService() {
		return forumContentService;
	}
	public void setForumContentService(ForumContentService forumContentService) {
		this.forumContentService = forumContentService;
	}
	public ForumContentReply getForumContentReply() {
		return forumContentReply;
	}
	public void setForumContentReply(ForumContentReply forumContentReply) {
		this.forumContentReply = forumContentReply;
	}
	public ForumContentReplyService getForumContentReplyService() {
		return forumContentReplyService;
	}
	public void setForumContentReplyService(
			ForumContentReplyService forumContentReplyService) {
		this.forumContentReplyService = forumContentReplyService;
	}
	public List<ForumContentReply> getReply() {
		return reply;
	}
	public void setReply(List<ForumContentReply> reply) {
		this.reply = reply;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	@Action("listForumContent")
	public String listForumContent(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ForumContent.class);
		criteria.add(Restrictions.eq("forumManager", forumManager));
		criteria.addOrder(Order.asc("guding"));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getForumContentService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editForumContent")
	public String editForumContent(){
//		forum=this.getForumManagerService().getAllForumManager();
		return SUCCESS;
	}
	@Action("viewForumContentReply")
	public String viewForumContentReply(){
		this.reply=this.forumContent.getForumContentReply();
		this.title=this.forumContent.getTitle();
		this.forumContent.setReadyNum(forumContent.getReadyNum()+1);
		this.getForumContentService().submit(forumContent);
		return SUCCESS;
	}
	@Action("editForumContentReply")
	public String editForumContentReply(){
		if (forumContent!=null) {
			this.title=this.forumContent.getTitle();
		}
		this.reply=this.getForumContentReplyService().getAllContentReply();
		return SUCCESS;
	}
	@Action(value = "saveDiary", results = {
			@Result(name = "success", type = "redirect", location = "listForumContent?forumManager=${forumContent.getForumManager().getId()}"),
			@Result(name = "error", type = "chain", location = "editForumContent?forumManager=${forumContent.getForumManager().getId()}") })
	public String saveForumContent(){
		if(upload!=null){
			this.forumContent.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		Person person=new Person();
		person=AppContext.getAccountContext().currentUser();
		forumContent.setLastPerson(person.getResume().getName());
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		forumContent.setLastTime(sf.format(date));
		if(forumContent.getId()!=null){
			if(forumContent.getForumManager()!=null){
				this.getForumManagerService().submit(forumContent.getForumManager());
			}
			forumContent.getForumManager().setLastaArticle(forumContent.getTitle());
			forumContent.getForumManager().setLastDate(forumContent.getLastTime());
			forumContent.getForumManager().setLastName(forumContent.getLastPerson());
			forumContent.setForumManager(forumContent.getForumManager());
			this.getForumContentService().submit(forumContent);
		}else{
			forumContent.setType(0);
			forumContent.setReplyNum(0);
			forumContent.setReadyNum(0);
			forumContent.setGuding(2);
			if(forumManager!=null){
				this.getForumManagerService().submit(forumManager);
			}
			forumManager.setThemeNum(forumManager.getThemeNum()+1);
			forumManager.setArticleNum(forumManager.getArticleNum()+1);
			forumManager.setLastaArticle(forumContent.getTitle());
			forumManager.setLastDate(forumContent.getLastTime());
			forumManager.setLastName(forumContent.getLastPerson());
			forumContent.setForumManager(forumManager);	
			this.getForumContentService().submit(forumContent);
			this.setPrompt("添加成功！");
		}
		return SUCCESS;
	}
	@Action(value = "saveForumContentReply", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContent.getId()}"),
			@Result(name = "error", type = "chain", location = "editForumContentReply?forumContent=${forumContent.getId()}") })
	public String saveForumContentReply(){
		if(upload!=null){
			this.forumContentReply.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		forumContent.setReplyNum(forumContent.getReplyNum()+1);
//		if(forumManager!=null){
//		forumManager.setArticleNum(1);
//		this.getForumManagerService().submit(forumManager);
//		}
		Person person=AppContext.getAccountContext().currentUser();
		forumContent.setLastPerson(person.getResume().getName());
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		forumContent.setLastTime(sf.format(date));
		this.getForumContentService().submit(forumContent);
		if (forumContentReply.getId()==null) {
			forumContentReply.setForumContent(forumContent);
			this.forumContentReply.setTitle(title);
			this.forumContentReplyService.save(forumContentReply);
			forumContent.getForumManager().setArticleNum(forumContent.getForumManager().getArticleNum()+1);
			this.getForumManagerService().submit(forumContent.getForumManager());
		}else{
			this.forumContentReplyService.submit(forumContentReply);
		}
		return SUCCESS;
	}
	@Action(value = "removeForumContentReply", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContentReply.getForumContent().getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContentReply.getForumContent().getId()}") })
	public String removeForumContentReply(){
		this.getForumContentReplyService().remove(forumContentReply);
		forumContentReply.setForumContent(forumContentReply.getForumContent());
		forumContentReply.getForumContent().setReplyNum(forumContentReply.getForumContent().getReplyNum()-1);
		this.getForumContentService().submit(forumContentReply.getForumContent());
		return SUCCESS;
	}
	@Action(value = "removeForumContent", results = {
			@Result(name = "success", type = "redirect", location = "listForumContent?forumManager=${forumContent.getForumManager().getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContent.getId()}") })
	public String removeForumContent(){
		if(forumContentReply!=null){
			this.getForumContentReplyService().remove(forumContentReply);
		}
		this.getForumContentService().remove(forumContent);
		forumContent.getForumManager().setArticleNum(forumContent.getForumManager().getArticleNum()-1);
		forumContent.getForumManager().setThemeNum(forumContent.getForumManager().getThemeNum()-1);
		this.forumManagerService.save(forumContent.getForumManager());
		return SUCCESS;
	}
	@Action(value = "saveForumHot", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContent.getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContent.getId()}") })
	public String saveForumHot(){
		forumContent.setType(1);
		this.forumContentService.save(forumContent);
		return SUCCESS;
	}
	@Action(value = "saveForumComm", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContent.getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContent.getId()}") })
	public String saveForumComm(){
		forumContent.setType(0);
		this.forumContentService.save(forumContent);
		return SUCCESS;
	}
	@Action(value = "saveForumLock", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContent.getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContent.getId()}") })
	public String saveForumLock(){
		forumContent.setType(2);
		this.forumContentService.save(forumContent);
		return SUCCESS;
	}
	@Action(value = "saveForumClear", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContent.getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContent.getId()}") })
	public String saveForumClear(){
		forumContent.setType(0);
		this.forumContentService.save(forumContent);
		return SUCCESS;
	}
	@Action(value = "saveForumTop", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContent.getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContent.getId()}") })
	public String saveForumTop(){
		forumContent.setType(3);
		forumContent.setGuding(1);
		this.forumContentService.save(forumContent);
		return SUCCESS;
	}
	@Action(value = "saveForumTops", results = {
			@Result(name = "success", type = "redirect", location = "viewForumContentReply?forumContent=${forumContent.getId()}"),
			@Result(name = "error", type = "chain", location = "viewForumContentReply?forumContent=${forumContent.getId()}") })
	public String saveForumTops(){
		forumContent.setType(0);
		forumContent.setGuding(2);
		this.forumContentService.save(forumContent);
		return SUCCESS;
	}
}
